<?php

namespace app\admin\controller\video\list;

use app\BaseController;
use app\model\video\VideoCollectModel;
use app\model\video\VideoModel;
use app\model\video\VideoTypeModel;
use think\App;

class Index extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 类型列表
     * @return bool|string
     */
    public function TypeList(): bool|string
    {
        try {
            $param = $this->request->param();
            $where = [];
            if (!empty($param['name'])) {
                $where[] = ['name', 'like', '%' . $param['name'] . '%'];
            }
            $where[] = ['unit_id','=',$this->userData['unit_id']];
            $VideoTypeModel = new VideoTypeModel();
            $data = $VideoTypeModel->where($where)->withCount(['Video'])->order('sort','asc')->select()->toArray();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 视频列表
     * @return bool|string
     */
    public function index(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'pageSize|每页数量' => 'require|integer',
            ]);
            $where = [];
            if (!empty($param['name'])) {
                $where[] = ['name', 'like', '%' . $param['name'] . '%'];
            }
            if (!empty($param['type_id'])) {
                $where[] = ['type_id', '=', $param['type_id']];
            }
            $where[] = ['unit_id','=',$this->userData['unit_id']];
            $VideoModel = new VideoModel();
            $data = $VideoModel->where($where)->with(['Type'])->paginate($param['pageSize'])->toArray();
            $CollectModel = new VideoCollectModel();
            $collect_id = $CollectModel->where('uid','=',$this->userData['id'])->column('video_id');
            foreach ($data['data'] as &$value) {
                $value['label'] = explode(',', $value['label']);
                if (in_array($value['id'], $collect_id)) {
                    $value['is_collect'] = 1;
                }else{
                    $value['is_collect'] = 0;
                }
            }
            $field = array_column($data['data'],'is_collect');
            array_multisort($field, SORT_DESC, $data['data']);
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 视频收藏
     * @return bool|string
     */
    public function collect(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $CollectModel = new VideoCollectModel();
            $CollectModel->insert([
                'uid' => $this->userData['id'],
                'video_id' => $id,
                'time' => time()
            ]);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 视频取消收藏
     * @return bool|string
     */
    public function cancelCollect(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $CollectModel = new VideoCollectModel();
            $info = $CollectModel->where([
                ['uid', '=', $this->userData['id']],
                ['video_id', '=', $id],
            ])->find();
            if (!$info) throw new \Exception('取消失败！');
            $info->is_del = 1;
            $info->del_time = time();
            $info->save();
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }
}